const db = require('../db')
const R = require('../response')

exports.getColumnList = (req, res) => {
    const query = req.query
    const currentPage = query?.currentPage ? query.currentPage : 1
    const pageSize = query?.pageSize ? query.pageSize : 5
    const sqlStr = 'select * from tb_column limit ?, ?'
    db.query(sqlStr, [(currentPage -1) * pageSize, pageSize], (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        // 获取专栏总数
        const totalSql = 'select count(*) as count from tb_column'
        db.query(totalSql, '', (err, total) => {
            return res.send(new R(0, '获取专栏列表成功!', {
                columns: results,
                total: total[0].count
            }))
        })
    })
}

exports.addColumn = (req, res) => {
    // 查看该专栏是否已经存在
    const columnStr = 'select * from tb_column where title = ? and user_id = ?'
    db.query(columnStr, [req.body.title, req.body.userId], (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        if (results.length > 0) {
            return res.send(new R(1, '该专栏已经存在,请添加其他专栏!'))
        }
        const sqlStr = 'insert into tb_column set ?'
        db.query(sqlStr, { title: req.body.title, description: req.body.description,
            cover: req.body.cover, user_id: req.body.userId, createdAt: new Date() }, (err, results) => {
                if (err) {
                    return res.send(new R(1, err.message))
                }
                if (results.affectedRows !== 1) {
                    return res.send(new R(1, '添加专栏失败!'))
                }
                return res.send(new R(0, '添加专栏成功!'))
        })
    })
}

exports.getColumnDetail = (req, res) => {
    const columnId = req.params.id
    const sqlStr = 'select * from tb_column where id = ?'
    db.query(sqlStr, columnId, (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        if (results.length !== 1) {
            return res.send(new R(1, '没有此专栏!'))
        }
        return res.send(new R(0, '获取专栏详情成功!', results[0]))
    })
}

exports.getUserColumns = (req, res) => {
    const userId = req.params.userId
    const sqlStr = 'select * from tb_column where user_id = ?'
    db.query(sqlStr, userId, (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        res.send(new R(0, '获取用户专栏成功!', results))
    })
}

exports.updateColumnInfo = (req, res) => {
    const columnInfo = req.body
    const sqlStr = 'update tb_column set ? where id = ?'
    db.query(sqlStr, [columnInfo, columnInfo.id], (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        if (results.affectedRows !== 1) {
            return res.send(new R(1, '更新专栏信息失败!'))
        }
        return res.send(new R(0, '更新专栏成功!'))
    })
}

exports.getAllColumns = (req, res) => {
    const sqlStr = 'select * from tb_column'
    db.query(sqlStr, '', (err, results) => {
        if (err) {
            return res.send(new R(1, err.message))
        }
        return res.send(new R(0, '获取所有专栏列表成功!', results))
    })
}